package com.kitchen.ui.deco
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	internal class BackGround extends Shape{
		public var deco:Object = null;
		private var bitdata:BitmapData;
		private var loader:Loader;
		public var w:int;
		public var h:int;
		
		public function BackGround(w:int, h:int){
			this.w = w;
			this.h = h;
		}
		
		public function setBgImage(deco:Object):void{
			this.deco = deco;
			this.loader = new Loader();
			this.loader.load(new URLRequest(deco.sUrl));
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioeHandler);
		}
		
		private function loadedHandler(event:Event):void{
			this.bitdata = Bitmap(this.loader.content).bitmapData;
			this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadedHandler);
			this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioeHandler);
			this.loader = null;
			this.paint();
		}
		
		private function ioeHandler(event:IOErrorEvent):void{
			this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadedHandler);
			this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioeHandler);
			this.loader = null;
		}
		
		private function paint():void{
			this.graphics.clear();
			this.graphics.lineStyle(1, 0xffffff, 0.001);
			this.graphics.beginBitmapFill(this.bitdata);
			this.graphics.drawRect(0, 0, this.w, this.h);
			this.graphics.endFill();
		}
	}
}